package com.hd.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.hd.mapper.ActivityMapper;
import com.hd.service.ActivityService;
import com.hd.dto.ActivityDTO;
import com.hd.entity.Activity;
import com.hd.entity.ActivityCategory;
import com.hd.entity.City;
import enums.ActivityCategoryEnum;
import enums.ActivityCityEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;

    /**
     * 获取初始化数据
     * @return
     */
    @Override
    public Map<String,Object> init() {
        //获取全部活动枚举类型
        List<ActivityCategoryEnum> categoryEnums = ActivityCategoryEnum.getAllCategory();
        //该集合用于封装枚举结果  Category:id-name
        List<ActivityCategory> categoryList=new ArrayList<>();
        //遍历枚举类型，把活动分类添加至集合中
        for (ActivityCategoryEnum categoryEnum : categoryEnums) {
            categoryList.add(new ActivityCategory(categoryEnum.getId(),categoryEnum.getName()));
        }

        //同上，城市类型
        List<ActivityCityEnum> cityEnums = ActivityCityEnum.getAllCity();
        List<City> cityList=new ArrayList<>();
        for (ActivityCityEnum cityEnum : cityEnums) {
            cityList.add(new City(cityEnum.getId(),cityEnum.getName()));
        }

        //封装至Map集合中，返回结果
        Map<String, Object> initMap = new HashMap<>();
        initMap.put("categoryList",categoryList);
        initMap.put("cityList",cityList);
        return initMap;
    }

    /**
     * 提交活动数据
     * @return
     */
    @Override
    public int submit(ActivityDTO activityDTO) {
        Activity activity=new Activity();
        BeanUtil.copyProperties(activityDTO,activity);
        //返回插入的主键
        return  activityMapper.insert(activity);
    }
}
